import hashlib
import time
import re
# 操作html界面的
from selenium.webdriver.common.by import By

from automate_testing.operation.bankcard_operate import get_userId
from automate_testing.tool.DButils import DButils
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class LoanOperate:

    @staticmethod
    def apply_loan(driver,loan_money,loan_term):

        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH,'//*[@id="amount"]').send_keys(loan_money)
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(loan_term)
        driver.find_element(By.XPATH,'//*[@id="submit"]').click()
        time.sleep(1)
        confirm_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]'))
        )
        confirm_button.click()
        time.sleep(3)

    @staticmethod
    def update_user_sql(username,realname,IDcard,phone,email):
        update_sql='update user set realname=%s,IDcard=%s,phone=%s,email=%s where username=%s;'
        DButils().update(update_sql,(realname,IDcard,phone,email,username))


    @staticmethod
    def get_loan_record(username, amount,term):
        userId=get_userId(username)
        # 多了一条贷款记录
        select_sql='SELECT amount, term FROM loan WHERE userId = %s AND id = (SELECT MAX(id) FROM loan WHERE userId = %s);'
        data=DButils().select(select_sql, (userId,userId))
        if len(data)!=0 and data[0][0]==amount and data[0][1]==term:
            return True
        else:
            return False
    @staticmethod
    def examine_loan(driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/ul/li[1]/a').click()
        time.sleep(1)
        # 等待页面加载完成
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(1)

    @staticmethod
    def get_examine_loan_result(username, amount, term):
        userId=get_userId(username)
        select_sql = 'SELECT applyStatus FROM loan WHERE userId = %s AND id = (SELECT MAX(id) FROM loan WHERE userId = %s) AND amount=%s AND term=%s;'
        data = DButils().select(select_sql, (userId, userId, amount, term))
        # print(data)
        if len(data)!=0 and data[0][0]==2:
            return True
        else:
            return False
"""
mysql> desc loan;
+-------------+---------------+------+-----+---------+----------------+
| Field       | Type          | Null | Key | Default | Extra          |
+-------------+---------------+------+-----+---------+----------------+
| id          | int           | NO   | PRI | NULL    | auto_increment |
| userId      | int           | YES  |     | NULL    |                |
| adminId     | int           | YES  |     | NULL    |                |
| startTime   | datetime      | YES  |     | NULL    |                |
| amount      | decimal(10,2) | YES  |     | NULL    |                |
| term        | int           | YES  |     | NULL    |                |
| rate        | decimal(10,8) | YES  |     | NULL    |                |
| applyStatus | int           | YES  |     | NULL    |                |
| loanStatus  | int           | YES  |     | NULL    |                |
+-------------+---------------+------+-----+---------+----------------+
9 rows in set (0.03 sec)

mysql> select * from loan;
+----+--------+---------+---------------------+------------+------+------------+-------------+------------+
| id | userId | adminId | startTime           | amount     | term | rate       | applyStatus | loanStatus |
+----+--------+---------+---------------------+------------+------+------------+-------------+------------+
|  7 |      1 |       1 | 2021-02-14 01:27:58 |   10000.00 |   20 | 0.15000000 |           2 |          2 |
|  8 |      1 |       1 | 2021-02-14 02:03:09 |   10000.00 |   20 | 0.15000000 |           2 |          2 |
|  9 |      1 |       1 | 2021-02-14 02:04:39 |   10000.00 |   20 | 0.15000000 |           2 |          2 |
| 10 |      1 |       1 | 2021-02-14 02:16:29 |   10000.00 |   20 | 0.15000000 |           2 |          2 |
| 11 |      1 |       1 | 2021-02-14 02:21:40 |   10000.00 |   20 | 0.15000000 |           2 |          2 |
"""